package com.yh.controller;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;






import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yh.bean.BuyGoods;
import com.yh.bean.User;
import com.yh.service.BuyGoodsService;


@Controller
public class BuyGoodsController {
	
	@Autowired
	private BuyGoodsService buyGoodsService;
	
	//删除订单
	@RequestMapping("deleteBuyGoods")
	public String deleteBuyGoods(int bid){
		BuyGoods buyGoods=new BuyGoods();
		buyGoods.setBid(bid);
		buyGoodsService.deleteBuyGoods(buyGoods);
		return "redirect:Order.jsp";
	}
	
	//添加订单
	@RequestMapping("addBuyGoods")
	public String addBuyGoods(){
		buyGoodsService.addBuyGoods();
		return "redirect:Order.jsp";
	}
	
	//显示订单列表
	@RequestMapping("listBuyGoods")
	@ResponseBody
	public List<BuyGoods> listBuyGoods(){
		List<BuyGoods> listBuyGoods=buyGoodsService.listBuyGoods();
		System.out.println("=============进入listBuyGoods");
		//ModelAndView mav=new ModelAndView();
		//mav.addObject("listBuyGoods", listBuyGoods);
		//mav.setViewName("../Order");
		return listBuyGoods;
	}
	
	//添加指定商品到订单
	@RequestMapping("addBuyGoodsByGoods")
	@ResponseBody
	public BuyGoods addBuyGoodsByGoods(int g_gz_gc_id,double gprice,int gsum,String gtime,int u_Id,String gphoto,String gdetail,String goodsAllClass){
		BuyGoods buyGoods=new BuyGoods();
		buyGoods.setBphoto(gphoto);
		buyGoods.setBprice(gprice);
		buyGoods.setBsum(gsum);
		buyGoods.setBtime(gtime);
		buyGoods.setG_gz_gc_id(g_gz_gc_id);
		buyGoods.setGdetail(gdetail);
		buyGoods.setU_id(u_Id);
		buyGoods.setBgoodsAllClass(goodsAllClass);
		System.out.println("=====通过ajax从页面获取======"+g_gz_gc_id+"===="+gprice+"===="+gsum+"===="+gdetail+"===="+gtime+"===="+goodsAllClass);
		//System.out.println("============buyGoods======="+buyGoods.getBphoto()+"===="+buyGoods.getGdetail());
		buyGoodsService.addBuyGoodsByGoods(buyGoods);
		return buyGoods;
	}
	
	//根据用户ID查询用户信息
	@RequestMapping("queryUser")
	@ResponseBody
	public Map queryUser(int u_Id,double gprice){
		User user=new User();
		user.setUid(u_Id);
		System.out.println("=================u_Id=="+u_Id);
		User user2=buyGoodsService.queryUser(user);
		//获取double类型的  用户余额
		double money=user2.getUmoney();
		//转化为  int类型
		int money2=new Double(money).intValue();
		//价格转化为  int类型
		int price=new Double(gprice).intValue();
		Map<String,Object> map=new HashMap<String,Object>();
		//判断用户余额是否大于商品价格
		if(money2>price){
			user2.setUmoney(money2-price);
			user2.setUid(u_Id);
			buyGoodsService.updateUserMoney(user2);
			map.put("money", user2.getUmoney());
		}else{
			map.put("error", "账户余额不足,请尽快充值");
		}
		System.out.println("用户购买后的余额============="+user2.getUmoney());
		System.out.println("用户余额============="+money2);
		System.out.println("商品价格============="+price);
		return map;
	}
	
	//购买商品增加用户积分
	@RequestMapping("increaseUserIntegral")
	@ResponseBody
	public Map increaseUserIntegral(int u_Id,double gprice){
		User user=new User();
		user.setUid(u_Id);
		User user2=buyGoodsService.queryUser(user);
		//获取用户当前积分
		int integral=user2.getIntegral();
		//价格转化为  int类型
		int price=new Double(gprice).intValue();
		//按商品价格的10%增加积分
		NumberFormat nf=NumberFormat.getInstance();
		nf.setMaximumFractionDigits(0);
		String result=nf.format((float)price/(float)10);
		int result2=Integer.parseInt(result);
		//购买后的积分
		int integral2=integral+result2;
		//修改该用户的积分
		user2.setIntegral(integral2);
		buyGoodsService.increaseUserIntegral(user2);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("integral", integral2);
		return map;
	}
}
